﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GdHotel.Data;
using GdHotel.Data.Helpers;
using GdHotel.Data.Interfaces;
using GdHotel.Data.Resources;
using UniqueTechnologies.Attributes;
using UniqueTechnologies.Data.Interfaces;

namespace GdHotel.Web.Controllers
{
    [LanguageSupport]
    [Authorize(Roles = "Admin")]
    public class ManageLanguagesController : BaseController
    {
        public override ILanguagesRepository LanguagesRepository { get; set; }

        public ManageLanguagesController(ILanguagesRepository languagesRepository)
        {
            LanguagesRepository = languagesRepository;
        }

        //
        // GET: /ManageLanguages/

        public ViewResult Index()
        {
            var Languages = LanguagesRepository.FindAll();
            return View(Languages);
        }

        //
        // GET: /ManageLanguages/Details/5

        public ViewResult Details(Int32 id)
        {
            var language = LanguagesRepository.GetById(id);
            return View(language);
        }

        //
        // GET: /ManageLanguages/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /ManageLanguages/Create

        [HttpPost]
        public ActionResult Create(Language language, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                //if (file != null)
                language.FlagPath = String.Empty;//file.SavePhoto(Common.FlagsDirectory, Server);
                LanguagesRepository.Insert(language);
                LanguagesRepository.Save();

                if (language.Default)
                    SetLanguageAsDefault(language);

                return RedirectToAction("Index");
            }

            return View(language);
        }

        //

        // GET: /ManageLanguages/Edit/5

        public ActionResult Edit(int id)
        {
            var language = LanguagesRepository.GetById(id);
            return View(language);
        }

        //

        // POST: /ManageLanguages/Edit/5

        [HttpPost]
        public ActionResult Edit(Language language, HttpPostedFileBase file)
        {
            var dbLanguage = LanguagesRepository.GetById(language.Id);
            if (ModelState.IsValid)
            {
                var photoPath = dbLanguage.FlagPath;
                if (file != null)
                    language.FlagPath = file.SavePhoto(Common.FlagsDirectory, Server);
                UpdateModel(dbLanguage);
                dbLanguage.FlagPath = file == null ? photoPath : file.SavePhoto(Common.FlagsDirectory, Server);
                LanguagesRepository.Save();

                if (language.Default)
                    SetLanguageAsDefault(language);

                return RedirectToAction("Index");
            }
            return View(language);
        }

        //

        // GET: /ManageLanguages/Delete/5

        public ActionResult Delete(int id)
        {
            var language = LanguagesRepository.GetById(id);
            return View(language);
        }

        //

        // POST: /ManageLanguages/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var language = LanguagesRepository.GetById(id);
            LanguagesRepository.Delete(language);
            LanguagesRepository.Save();
            return RedirectToAction("Index");
        }

        private void SetLanguageAsDefault(Language language)
        {
            var languages = LanguagesRepository.FindAll().Where(l => l.Id != language.Id);
            foreach (var otherLanguage in languages)
                otherLanguage.Default = false;
            LanguagesRepository.Save();
        }

        protected override void Dispose(bool disposing)
        {
            LanguagesRepository.Dispose();
            base.Dispose(disposing);
        }
    }
}